
#include <stdio.h>
#include <string.h>


#include "Type_def.h"
#include "STC32G.H"
#include "STC32G_GPIO.h"
#include "STC32G_UART.h"
#include "STC32G_NVIC.h"
#include "STC32G_Delay.h"
#include "STC32G_Switch.h"
#include "STC32G_WDT.h"

#include "app_config.h"
#include "app_common.h"
#include "app.h"
#include "debug.h"
#include "drv_jiffes.h"
#include "drv_irnec.h"
#include "drv_iouart.h"
#include "drv_network.h"
#include "drv_adc_sensors.h"

#if TCFG_DRV_ENCODER_SUPPORT
#include "drv_encoder.h"
#endif

#if TCFG_DRV_KEY_SUPPORT
#include "drv_key.h"
#endif

#if TCFG_DRV_INA219_SUPPORT
#include "drv_ina219.h"
#endif

#if TCFG_DRV_INA3221_SUPPORT
#include "drv_ina3221.h"
#endif

#if TCFG_DRV_LCD_SPI_SUPPORT
#include "lcd_draw_base.h"
#include "lcd_draw_bmp.h"
#include "lcd_draw_font.h"
#endif

#if (TCFG_DRV_SD_CARD_SPI_SUPPORT || TCFG_DRV_SPI_NORFLASH_SUPPORT)
#include "drv_sd.h"
#if TCFG_LIB_FATFS_SUPPORT
#include "ff.h"
#include "diskio.h"
#endif
#endif

#if TCFG_DRV_SPI_NORFLASH_SUPPORT
#include "drv_spi_norflash.h"
#endif
#if TCFG_DRV_COMPARE_SUPPORT
#include "drv_compare.h"
#endif



static struct app_t app = {0};

#if TCFG_DRV_IRNEC_SUPPORT
	static struct ir_keycode_t ir_keycode;
#endif

#if TCFG_DRV_SD_CARD_SPI_SUPPORT 
static FIL sd_fp;
static FATFS sd_fatfs;	// 必须是全局变量
static BYTE sd_work[FF_MAX_SS]; // 必须是全局变量
#endif

#if TCFG_DRV_SPI_NORFLASH_SUPPORT
#if TCFG_LIB_FATFS_SUPPORT
static FIL nor_fp;
static FATFS nor_fatfs; // 必须是全局变量
static BYTE nor_work[FF_MAX_SS]; // 必须是全局变量
#endif
#endif


void main(void)
{
	u8	i;
#if TCFG_DRV_ENCODER_SUPPORT
	uint8_t enc_value;
#endif
	key_code_t key_code;
	int32_t _jdiff;
	uint8_t buffer[64]={0};
	WDT_InitTypeDef wdt;

#if (TCFG_DRV_SD_CARD_SPI_SUPPORT || TCFG_DRV_SPI_NORFLASH_SUPPORT)
#if TCFG_LIB_FATFS_SUPPORT
	FRESULT res;
	FIL fp;
#endif
#endif

#if TCFG_CFG_WDT_ENABLE
	WDT_InitTypeDef	wdt;
#endif

	WTST = 0;		//设置程序指令延时参数，赋值为0可将CPU执行指令的速度设置为最快
	EAXSFR();		//扩展SFR(XFR)访问使能 
	CKCON = 0;      //提高访问XRAM速度


	wdt.WDT_Enable = ENABLE;
	wdt.WDT_IDLE_Mode = WDT_IDLE_RUN;
	wdt.WDT_PS = WDT_SCALE_64;
	WDT_Inilize(&wdt);

#if (TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_4G)||(TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_NB)	
	app.nb_psmmode = 0;	// NB模块PSM模式设置标志
	app.mqttrecon = 0; // 1:MQTT重新连接，0：第一次连接
	app.netinit_step = E_NET_INIT_STEP_0;
	app.rtcntp_synced = 0;	// 必须初始化为0
	app.status_synced = 0;  // 必须初始化为0

	// TODO: 网络模块相关
	app.network.at_ready = 0;
	app.network.simcardin = 0;
	app.network.reg_mode = 0;
	app.network.reg_stat = 0;
#endif

	app.pjiffes = jiffes_handler();

	// TODO: 硬件初始化
	debug_uart_config();	// 打印串配置
	drv_jiffes_config();	// 系统jiffes基准时间定时器初始化1ms定时

	EA = 1;	// 使能总中断

	// TODO: 应用初始化
	app_init(&app);
	
#if TCFG_CFG_WDT_ENABLE
	wdt.WDT_Enable = ENABLE;
	wdt.WDT_IDLE_Mode = WDT_IDLE_RUN;
	wdt.WDT_PS = WDT_SCALE_16;
	WDT_Inilize(&wdt);
	WDT_reset(100);
#endif

	// TODO: 模块初始化
#if (TCFG_DRV_ADC_NTC_SENSOR_SUPPORT || TCFG_DRV_ADC_VOL_SENSOR_SUPPORT || TCFG_DRV_ADC_UV_SENSOR_SUPOORT)
	drv_adc_sensors_init();	/* ADC 初始化 */
#endif

#if ((TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_NB)||(TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_4G))
	network_module_init(&app);
#endif

#if TCFG_DRV_IRNEC_SUPPORT
	ir_nec_driver_init();
#endif

#if TCFG_DRV_ENCODER_SUPPORT
	drv_encoder_init();
#endif

#if TCFG_DRV_KEY_SUPPORT
	drv_key_init();
#endif

#if TCFG_DRV_IOUART_SUPPORT
	io_uart_driver_init();
#endif

#if TCFG_DRV_INA219_SUPPORT
	drv_ina219_init();
#endif

#if TCFG_DRV_INA3221_SUPPORT
	drv_ina3221_init();
#endif

	// 内部比较强初始化
#if TCFG_DRV_COMPARE_SUPPORT
	drv_compare_init();
#endif
	
	// 内部RTC初始化
	rtc_init();

#if  TCFG_DRV_SPI_NORFLASH_SUPPORT
	NorFlash_GPIO_Config();	// 设置NORFLASH SPI GPIO端口
	NorFlash_Init();
#if TCFG_LIB_FATFS_SUPPORT
	res = f_mount(&nor_fatfs, "3:", 1);	// 挂载SD卡，0=DEV_RAM, 1=DEV_MMC, 2=DEV_USB, 3=DEV_NOR
	log_d("norflash f_mount res:%d\n", res);
	if (res == FR_NO_FILESYSTEM) {
		// 无文件系统，尝试创建文件系统
		res = f_mkfs("3:", 0, nor_work, sizeof(nor_work));
		if (FR_OK == res) {
			/* 格式化后，先取消挂载 */
			f_mount(NULL, "3:", 1);	// 取消文件系统
			res = f_mount(&nor_fatfs, "3:", 1);	// 挂载文件系统
			log_d("norflash f_mount  222 res:%d\n", res);						
		}
	}

	if (FR_OK == res) {
		log_d(" Norflash Mount success.\n");	
	}
#endif
#endif

#if TCFG_DRV_LCD_SPI_SUPPORT
	lcd_draw_init();

	// lcd_draw_string(char *str, int x, int y, u8 font_size, u8 wordspace, u8 align, uint8_t draw_inverse, u8 draw_mode)
	// lcd_draw_string("0123456789", 0, 0, E_FONT_S16X24, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_SC);

	lcd_draw_fill(E_DRAW_0, E_DRAW_FB);	// 清除BUF
	// lcd_draw_fill_rect(0, 0, 64, 64, E_DRAW_1, E_DRAW_FB);
	// lcd_draw_fill_rect(30, 20, 15, 15, E_DRAW_1, E_DRAW_FB);
	
	// lcd_draw_string("报警阈值", 0, 0, E_FONT_S16X16, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
	// lcd_draw_string("01234abcABC", 0, 16, E_FONT_S08X16, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
	// lcd_draw_string("01234abcABC", 0, 32, E_FONT_S16X24, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);

	// lcd_draw_bmp(0, 0, IMG_DATA_MODE_SHIZI, E_DRAW_NORMAL, E_DRAW_FB);
	lcd_draw_update();
#endif

	PrintString1("STC32G UART1 Test Programme!\r\n");	//UART1发送一个字符串	
	printf("Hello Test Demo:%d\n", 123);
	log_d("F:%f, D:%d\n", 1.23, 123);
	
	// app.pjiffes->s = 0x12345678; // for test

#if TCFG_DRV_SD_CARD_SPI_SUPPORT
	SD_GPIO_Config();	// 设置SD SPI GPIO口
	#if TCFG_LIB_FATFS_SUPPORT
	res = f_mount(&sd_fatfs, "1:", 1);	// 挂载SD卡，1=DEV_MMC
	log_d("f_mount res:%d\n", res);
	if (res == FR_NO_FILESYSTEM) {
		// 无文件系统，尝试创建文件系统
		res = f_mkfs("1:", 0, sd_work, sizeof(sd_work));
		res = f_mount(NULL, "1:", 1);	// 取消文件系统
		res = f_mount(&sd_fatfs, "1:", 1);	// 挂载文件系统
		log_d("f_mount 222 res:%d\n", res);
	}
	#endif
#endif


	while (1)
	{
		// TODO: 看门狗清零
		WDT_Clear();

		// TODO: RTC任务处理
		rtc_task();

#if TCFG_DRV_COMPARE_SUPPORT
		drv_compare_task();
#endif

#if ((TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_NB)||(TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_4G))
		// 4G Cat1特有 PWRKEY 拉低2.5S开机
		#if TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_4G
		if (E_NET_PWRKEY_STEP_INIT == app.network.pwrkey_step) {
			log_d("PWRKEY LOW\n");
			CAT1_PWRKEY_LOW();  // 4G 模块PWRKEY拉低
			jiffes_now_add_ms(&app.network.jpwrkeytime, 2500);
			app.network.pwrkey_step = E_NET_PWRKEY_STEP_L;		
		} else if ((E_NET_PWRKEY_STEP_L ==  app.network.pwrkey_step) && jiffes_compare_now(&app.network.jpwrkeytime) >= 0) {
			log_d("PWRKEY HIGH\n");
			CAT1_PWRKEY_HIGH();  // 4G 模块PWRKEY拉低
			app.network.pwrkey_step = E_NET_PWRKEY_STEP_H;	
		}
		#endif
		
		// TODO: 网络重连 模块复位脚时序处理
		if ((E_NET_INIT_STEP_1 == app.netinit_step) && jiffes_compare_now(&app.network.jnetrst) >= 0) {
			#if TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_4G
			log_d("======>4G RESET HIGH\n");
			// NET_POWER_ON();
			NET_RST_HIGH();
			app.netinit_step = E_NET_INIT_STEP_2;
			#elif TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_NB
			log_d("======>NB RESET HIGH\n");
			// NET_POWER_ON();
			NET_RST_HIGH();
			app.netinit_step = E_NET_INIT_STEP_2;
			#endif
			app.network.net_connecting = 1; 
		}

		// TODO: 网络准备好，网络已注册注册，网络附属成功，信号正常
		if (((1 == app.network.reg_stat || 5 == app.network.reg_stat)) && (app.network.rssi > TCFG_RSSI_LOSS_VALUE)) {

			#if TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_NB
			// TODO: NB模块设置PSM模式 0关闭PSM模式
			if (app.nb_psmmode == 0) {
				app.nb_psmmode = 1;
				nbiot_psmmode_atcmd_send(&app, 0);
			}
			#endif		
			
			// TODO: MQTT连接对信号RSSI有要求，设定大于某个值（5~15）才可以正常使用网络  RSSI:0~31  99网络不可用
			if ((app.network.rssi >= TCFG_RSSI_GOOD_VALUE)&&(app.network.rssi <= TCFG_RSSI_MAX_VALUE)) {				
				// TODO: 检查每日同步一次RTC时间， RTC中断设为日中断

				#if !TCFG_SYNCTIME_BY_MODULE
				if (E_NET_INIT_STEP_0 == app.netinit_step) {
					app.netinit_step = E_NET_INIT_STEP_2;
				}
				#endif

				if (app.netinit_step == E_NET_INIT_STEP_2) {
					app.netinit_step = E_NET_INIT_STEP_3;
					jiffes_get(&app.network.jnetrst);	// 获取当前jiffes时间, 用于后面处理时间判断
				} else {
					if (E_NET_INIT_STEP_3 == app.netinit_step) {
						_jdiff = jiffes_diff_now(&app.network.jnetrst);
						if (_jdiff >= 3*SECOND) {
							app.netinit_step =E_NET_INIT_STEP_4;
							network_mqtt_connect_atcion(&app);
						}
					}
				}
			}

		}

		// TODO: 网络模块  状态检查 NTP时间同步检查, 如果有判断(app.network.at_ready==1) 会导致运行过程中网络异常重连时不会发送网络检查指令
		if ((jiffes_compare_now(&app.jnetcheck) >= 0)) {
			uint32_t _delay = 0;
			if (E_NET_INIT_STEP_4 == app.netinit_step) {
				_delay = 30*SECOND;		// 已执行MQTT连接命令 30S
			} else {
				_delay = 10*SECOND;		// 未执行MQTT连接命令 10S
			}
			jiffes_now_add_ms(&app.jnetcheck, _delay);	

			#if TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_4G
				cat1_network_check_atcmd_send(&app);	// 检查网络AT命令
			#elif TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_NB
				nbiot_network_check_atcmd_send(&app);	// 检查网络AT命令
			#endif

			// TODO: 开机是否完成一次基站授时
			#if TCFG_SYNCTIME_BY_MODULE
			if (0 == app.rtcntp_synced) {
				#if TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_4G
				cat1_ctzu_get_atcmd_send(&app);	// 获取自动更新时间设置值
				#elif TCFG_NETWORK_MODULE_TYPE == NETWORK_MODULE_NB
				#endif
				rtc_sync_cclk_set_atcmd_send(&app);
			}
			#endif			
		}

		// TODO: MQTT 上报数据
		if ((jiffes_compare_now(&app.jmqtt_pub) >= 0)&&(1==app.network.mqtt_connected)) {
			jiffes_now_add_ms(&app.jmqtt_pub, TCFG_REPORT_INTERVAL_MS);
						
			if (0 == app.status_synced ) {
				// TODO:MQTT服务连接后第一个心跳包，不发送报文，可能MQTT还没有连接完成
				app.status_synced = 1;	
				log_d("sync wait...\n");							
			} else if (1 == app.status_synced) {
				app.status_synced = 2;
				log_d("sync E_MQTT_MSG_TYPE_VALUE\n");
				app.network.opt_type = 1;	// 1:阈值同步，2：阈值设置
			} else  {	

			}			
		}

		// TODO: 检查网络MQTT重连
		network_checking_reconnect(&app);

		// TODO: 网络模块AT命令收发任务
		network_atcmd_process_task(&app);
#endif


#if TCFG_DRV_IRNEC_SUPPORT
		// TODO: NEC协议红外遥控接收解码
		ir_keycode = ir_nec_driver_keycode();
		if (ir_keycode.repeat) {
			log_d("Repeat\n");
			ir_keycode.isvalid = 0;
		}
		if (ir_keycode.isvalid) {
			switch (ir_keycode.keycode) {
			case IR_KEYCODE_PWR:
			{
				log_d("IR_KEYCODE_PWR\n");
				#if TCFG_DRV_SPI_NORFLASH_SUPPORT
				// NorFlash_Init();
				#endif
			}
				break;
			case IR_KEYCODE_MENU:
			{
				log_d("IR_KEYCODE_MENU\n");
				#if TCFG_DRV_SPI_NORFLASH_SUPPORT
				NorFlash_ReadStatus();			
				#endif	
			}					
				break;
			case IR_KEYCODE_TEST:
				log_d("IR_KEYCODE_TEST\n");
				lcd_draw_clear(E_DRAW_0);		
				lcd_draw_string("中华人民共和国家", 0, 0, E_FONT_S12X12, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				lcd_draw_string("中华人民共和国家", 0, 16, E_FONT_S16X16, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				lcd_draw_string("中华人民共和国人民解放军万岁", 0, 100, E_FONT_S24X24, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				lcd_draw_string("赢", LCD_WIDTH-30, LCD_HEIGH-24, E_FONT_S24X24, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				// lcd_draw_chinese("国", 0, 0, E_FONT_S16X16, E_DRAW_NORMAL, E_DRAW_SC);
				lcd_draw_update();
				break;
			case IR_KEYCODE_BACK:
				log_d("IR_KEYCODE_BACK\n");		
				lcd_draw_clear(E_DRAW_0);
				lcd_draw_string("ABCabc0123@#$%^&", 0, 0, E_FONT_S12X12, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				lcd_draw_string("ABCabc0123@#$%^&", 0, 12, E_FONT_S16X16, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				lcd_draw_string("ABCabc0123@#$%^&", 0, 30, E_FONT_S24X24, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				lcd_draw_fill_rect(30,30,160,100, E_DRAW_1, E_DRAW_FB);
				lcd_draw_hline(0,152,240,E_DRAW_1, E_DRAW_FB);
				// lcd_draw_chinese("家", 16, 0, E_FONT_S16X16, E_DRAW_NORMAL, E_DRAW_SC);
				lcd_draw_update();
				break;

			case IR_KEYCODE_UP:
				log_d("IR_KEYCODE_UP\n");
				lcd_draw_clear(E_DRAW_0);
				lcd_draw_string("āáǎàōóǒòêēéěè℃", 0, 0, E_FONT_S12X12,  0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				lcd_draw_string("īíǐìūúǔùǖǘǚǜü℉", 0, 12, E_FONT_S16X16, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				lcd_draw_string("īíǐìūúǔùǖǘǚǜü℉", 0, 30, E_FONT_S24X24, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
				lcd_draw_update();
				break;
			case IR_KEYCODE_DOWN:
				log_d("IR_KEYCODE_DOWN\n");				
				lcd_draw_clear(E_DRAW_0);
				lcd_draw_circle(LCD_WIDTH/2, LCD_HEIGH/2, 5, 1, E_DRAW_FB);
				lcd_draw_circle(LCD_WIDTH/2, LCD_HEIGH/2, 10, 1, E_DRAW_FB);
				lcd_draw_circle(LCD_WIDTH/2, LCD_HEIGH/2, 20, 1, E_DRAW_FB);
				lcd_draw_circle(LCD_WIDTH/2, LCD_HEIGH/2, 30, 1, E_DRAW_FB);
				lcd_draw_circle(LCD_WIDTH/2, LCD_HEIGH/2, 40, 1, E_DRAW_FB);
				lcd_draw_circle(LCD_WIDTH/2, LCD_HEIGH/2, 50, 1, E_DRAW_FB);
				lcd_draw_circle(LCD_WIDTH/2, LCD_HEIGH/2, 60, 1, E_DRAW_FB);
				lcd_draw_update();
				break;
				
			case IR_KEYCODE_LEFT:
			{
				log_d("IR_KEYCODE_LEFT\n");
				lcd_draw_clear(E_DRAW_0);
				lcd_draw_line(0, 0, 0, 240-1, 1, E_DRAW_FB);	/* 水平线*/
				lcd_draw_line(0, 0, 160-1, 160-1, 1, E_DRAW_FB);    /* 斜线-从左到右45度 */
				lcd_draw_line(0, 240-1, 240-1, 0, 1, E_DRAW_FB);	/* 斜线-从右到左45度 */
				lcd_draw_line(120, 0, 120, 240-1, 1, E_DRAW_FB);	/* 垂直线 */
				lcd_draw_line(160-1, 0, 60, 240-1, 1, E_DRAW_FB);	/* 斜线-从左到右120度*/
				lcd_draw_line(60, 0, 160-1, 240-1, 1, E_DRAW_FB);	/* 斜线-从右到左60度*/
				lcd_draw_line(0, 60, 240-1, 160-1, 1, E_DRAW_FB);	/* 斜线-从左到右180度*/
				lcd_draw_line(0, 160-1, 240-1, 60, 1, E_DRAW_FB);	/* 斜线-从左到右30度*/
				lcd_draw_update();
			}				
				break;
			case IR_KEYCODE_RIGHT:
				log_d("IR_KEYCODE_RIGHT\n");
				lcd_draw_clear(E_DRAW_0);
				lcd_draw_rect(0, 0, LCD_WIDTH-1, LCD_HEIGH-1, 1, E_DRAW_FB);
				lcd_draw_update();
				break;
			case IR_KEYCODE_ENTER:
				log_d("IR_KEYCODE_ENTER\n");
				break;
			case IR_KEYCODE_C:
				log_d("IR_KEYCODE_C\n");				
				break;
			case IR_KEYCODE_0:
				log_d("IR_KEYCODE_0\n");
				#if TCFG_DRV_SPI_NORFLASH_SUPPORT
				// NorFlash_Erase(NORFLASH_ERASE_ALLCHIP, 0);
				#endif
				break;
			case IR_KEYCODE_1:
				log_d("IR_KEYCODE_1\n");
				break;
			case IR_KEYCODE_2:
				log_d("IR_KEYCODE_2\n");
				break;
			case IR_KEYCODE_3:
				log_d("IR_KEYCODE_3\n");
				break;
			case IR_KEYCODE_4:
				log_d("IR_KEYCODE_4\n");
				break;
			case IR_KEYCODE_5:
				log_d("IR_KEYCODE_5\n");
				break;
			case IR_KEYCODE_6:
				log_d("IR_KEYCODE_6\n");
				break;
			case IR_KEYCODE_7:
				log_d("IR_KEYCODE_7\n");
				break;
			case IR_KEYCODE_8:
				log_d("IR_KEYCODE_8\n");
				break;
			case IR_KEYCODE_9:
				log_d("IR_KEYCODE_9\n");
				break;
			default:
				break;
			}

			app.oled_refresh = 1;
		}
#endif

#if TCFG_DRV_ENCODER_SUPPORT
		enc_value = drv_encoder_value();
		if (E_ENCODER_N != enc_value) {			
			if (E_ENCODER_L == enc_value) {
				log_d("E_ENCODER_L\n");
			} else if (E_ENCODER_R == enc_value) {
				log_d("E_ENCODER_R\n");
			}
		}
#endif
		key_code = drv_key_value_get();
		if (key_code.key_type != KEYCODE_TYPE_NONE) {
			if (KEYCODE_TYPE_CLICK == key_code.key_type) {
				log_d("KEYCODE_TYPE_CLICK:%d\n", key_code.key_code);

			} else if (KEYCODE_TYPE_DCLICK == key_code.key_type) {
				log_d("KEYCODE_TYPE_DCLICK:%d\n", key_code.key_code);

			} else if (KEYCODE_TYPE_LONG_DOWN == key_code.key_type) {
				log_d("KEYCODE_TYPE_LONG_DOWN:%d\n", key_code.key_code);

			} else if (KEYCODE_TYPE_LONG_HOLD == key_code.key_type) {
				log_d("KEYCODE_TYPE_LONG_HOLD:%d\n", key_code.key_code);

			} else if (KEYCODE_TYPE_LONG_UP == key_code.key_type) {
				log_d("KEYCODE_TYPE_LONG_UP:%d\n", key_code.key_code);
				
			}
		}

		// TODO: 10ms 周期
		if (jiffes_compare_now(&app.jiffes_10MS) >=0) {
			jiffes_now_add_ms(&app.jiffes_10MS, 10); // 10ms

			#if TCFG_DRV_KEY_SUPPORT
			drv_key_scan();
			#endif
		}


		// TODO: 100ms 周期
		if (jiffes_compare_now(&app.jiffes_100MS) >=0) {
			jiffes_now_add_ms(&app.jiffes_100MS, 100); // 100ms

			#if TCFG_DRV_INA219_SUPPORT
			app.ina219_value.current = ina219_GetCurrent_uA();
			app.ina219_value.voltage = ina219_GetBusVoltage_mV();
			app.ina219_value.power = ina219_GetPower_mW();
			app.ina219_value.shunt = ina219_GetShuntVoltage_uV();
			#endif

			#if TCFG_DRV_INA3221_SUPPORT

			#endif

			
		}

		if (jiffes_compare_now(&app.jiffes_250MS) >=0) {
			jiffes_now_add_ms(&app.jiffes_250MS, 250); // 250ms
			
		}

		if (jiffes_compare_now(&app.jiffes_500MS) >=0) {
			jiffes_now_add_ms(&app.jiffes_500MS, 500); // 500ms
		
		#if 0
		#if 1
			lcd_draw_clear(E_DRAW_0);		
			sprintf(buffer, "电压：%.03fV", (float)(app.ina219_value.voltage/1000.0));
			lcd_draw_string(buffer, 0, 0, E_FONT_S12X12, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
			sprintf(buffer, "电流：%.03fA", (float)(app.ina219_value.current/1000000.0));
			lcd_draw_string(buffer, 0, 12, E_FONT_S12X12, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
			sprintf(buffer, "功率：%.03fW", (float)app.ina219_value.power/1000.0);
			lcd_draw_string(buffer, 0, 24, E_FONT_S12X12, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
			lcd_draw_update();
		#else
			lcd_draw_clear(E_DRAW_0);		
			sprintf(buffer, "电压：%dmV", app.ina219_value.voltage);
			lcd_draw_string(buffer, 0, 0, E_FONT_S12X12, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
			sprintf(buffer, "电流：%lduA", app.ina219_value.current);
			lcd_draw_string(buffer, 0, 12, E_FONT_S12X12, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
			sprintf(buffer, "功率：%ldmW", app.ina219_value.power);
			lcd_draw_string(buffer, 0, 24, E_FONT_S12X12, 0, TEXT_ALIGN_LEFT, E_DRAW_NORMAL, E_DRAW_FB);
			lcd_draw_update();
		#endif
		#endif
		}

		// TODO: 1S 周期
		if (jiffes_compare_now(&app.jiffes_1S) >=0) {
			jiffes_now_add_ms(&app.jiffes_1S, 1000); // 1000ms
			//log_d("s:%lu, ms:%ld\n", app.jiffes_1S.s, app.jiffes_1S.ms);		

			#if TCFG_DRV_ADC_UV_SENSOR_SUPOORT
			app.uv_level = drv_adc_sensors_uv_level_read();
			log_d("uv_level:%d\n", (int)app.uv_level);
			#endif

			#if TCFG_DRV_ADC_NTC_SENSOR_SUPPORT
			app.tempertaure = drv_ntc_tempertaure_get();
			log_d("tempertaure:%.02f\n", app.tempertaure);
			#endif

			
			#if 0//TCFG_DRV_INA219_SUPPORT
			// ina219_process();

			// TODO: 总线电压 Vin-和GND之间的电压
			log_d("ina219 voltage is:%d(mV)\r\n", app.ina219_value.voltage);
        
			// TODO: 分流电阻两端电压
			log_d("ina219 shunt   is:%ld(uV)\r\n",app.ina219_value.shunt);
				
			// TODO: 电流
			log_d("ina219 current is:%ld(uA)\r\n",app.ina219_value.current);
			
			log_d("ina219 power   is:%ld(mW)\r\n",app.ina219_value.power);		
			#endif
			
			#if TCFG_DRV_INA3221_SUPPORT
			ina3221_process();
			#endif
		}

		// TODO: 10S 周期
		if (jiffes_compare_now(&app.jiffes_10S) >=0) {
			jiffes_now_add_ms(&app.jiffes_10S, 10*1000); // 1000ms
			log_d("at cmd send 10s\n");			
		}


#if TCFG_APP_AT_COMMAND_SUPPORT
		// TODO: 应用AT命令支持
		if (jiffes_compare_now(&app.jiffes_dbgrecv) >=0) {
			jiffes_now_add_ms(&app.jiffes_dbgrecv, 1); // 1ms
			if(COM1.RX_TimeOut > 0)		//超时计数
			{
				if(--COM1.RX_TimeOut == 0)
				{ 
					
					// TODO: 串口命令解析
					if (strncmp(RX1_Buffer, "AT+", strlen("AT+")) == 0) {
						log_d("AT Command recv.\n");
					}
					
					// TODO: 串口命令回显
					if(COM1.RX_Cnt > 0)
					{
						for(i=0; i<COM1.RX_Cnt; i++)	TX1_write2buff(RX1_Buffer[i]);	//收到的数据原样返回
					}
					COM1.RX_Cnt = 0;
				}
			}
		}
#endif
	}
}
